package com.stgy.lease.web.api.controller;

import com.stgy.lease.common.utils.JwtUtil;
import com.stgy.lease.model.domain.SystemUser;
import com.stgy.lease.model.domain.UserInfo;
import com.stgy.lease.web.api.dto.UserLoginDto;
import com.stgy.lease.web.api.interceptor.StgyAuthInterceptor;
import com.stgy.lease.web.api.service.LoginService;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.web.api.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName:AppLoginController
 * Package:com.stgy.lease.api.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/4 - 15:59
 */
@RestController
@RequestMapping("/app")
@Tag(name = "会员登录模块")
public class AppLoginController {

    @Autowired
    private LoginService loginService;

    //http://localhost:8888/app/login/getCode?phone=13888888888 GET
    @Operation(summary = "发送短信验证码")
    @GetMapping("/login/getCode")
    public Result<String> getCode(@RequestParam String phone) {
        return Result.build(loginService.sendCode(phone));
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDto loginDto) {
        //登录成功 将用户信息创建为jwt token 返回
        return Result.ok(loginService.login(loginDto));
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public Result<UserInfoVo> info() {
        UserInfo userInfo = StgyAuthInterceptor.THREAD_LOCAL.get();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setAvatarUrl(userInfo.getAvatarUrl());
        userInfoVo.setNickname(userInfo.getNickname());
        return Result.ok(userInfoVo);
    }


}